using System;
using System.Collections;
using System.Text;

namespace Sedna.Core.Controller.Configurations
{
    public class ExecutionContextBasedColumnControlEnabler : IDataGridColumnControlEnabler
    {
        private bool enabled = true;
        private bool visible = true;
        
        private string enabledBindPath = null;
        private string visibleBindPath = null;

        private bool inverseEnabled;
        private bool inverseVisible;

        # region Properties

        public bool Enabled
        {
            set { enabled = value; }
        }

        public bool Visible
        {
            set { visible = value; }
        }

        public string EnabledBindPath
        {
            set { enabledBindPath = value; }
        }

        public string VisibleBindPath
        {
            set { visibleBindPath = value; }
        }

        public bool InverseEnabled
        {
            set { inverseEnabled = value; }
        }

        public bool InverseVisible
        {
            set { inverseVisible = value; }
        }

        # endregion

        public bool CheckIfEnabled(IDataGridColumnConfiguration columnConfiguration, object dataRow, IExecutionContext executionContext)
        {
            if (enabledBindPath != null && enabledBindPath.Length > 0)
            {
                object value = executionContext.GetPropertyValue(enabledBindPath);

                if (value != null)
                    enabled = (!inverseEnabled) ? Convert.ToBoolean(value) : !Convert.ToBoolean(value);
            }
            
            return enabled;
        }

        public bool CheckIfVisible(IDataGridColumnConfiguration columnConfiguration, object dataRow, IExecutionContext executionContext)
        {
            if (visibleBindPath != null && visibleBindPath.Length > 0)
            {
                object value = executionContext.GetPropertyValue(visibleBindPath);

                if (value != null)
                    visible = (!inverseVisible) ? Convert.ToBoolean(value) : !Convert.ToBoolean(value);
            }

            return visible;
        }
    }
}
